#include <vector>
using namespace std;
int candy(vector<int>& ratings) {
    int n = ratings.size();
    vector<int> ret1(n, 1), ret2(n, 1);
    for (int i = 1; i < n; ++i) {
        if (ratings[i] > ratings[i - 1])
            ret1[i] = ret1[i - 1] + 1;
    }
    for (int j = n - 2; j >= 0; --j) {
        if (ratings[j] > ratings[j + 1])
            ret2[j] = ret2[j + 1] + 1;
    }
    int ret = 0;
    for (int i = 0; i < n; ++i) {
        ret += max(ret1[i], ret2[i]);
    }
    return ret;
}
